import logging

import pytest

from resource.pom.settings import Settings
from resource.pom.system_ui import SystemUI
from utils.manager import Direction, Images


class Test:
    app = Settings
    system_ui = SystemUI

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-WLAN，开启WLAN')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.wait(1)
        device.dirc_fling(Direction.Up)
        device.click(*self.app.wlan_item)
        device.wait(3)
        device.refresh_layout()
        # 检查wifi开关状态
        toggle = device.get_element_by_type('Toggle')
        if toggle.get('checked') == 'false':
            device.click_element(toggle)
        logging.info('预期结果1.WLAN按钮被点亮，展示可用WLAN列表')
        device.wait(20)
        switch_area = device.get_element_area(toggle)
        switch_pic = device.save_snapshot_to_local('wifi_switch.jpeg')
        Images.crop_picture(switch_pic, *switch_area)
        similarity = Images.compare_image_similarity(switch_pic, self.app.switch_on)
        assert similarity > 0.8, 'wifi开关未点亮'
        device.refresh_layout()
        device.assert_text_exist('可用WLAN')
        device.assert_text_exist('加密')
        device.assert_text_exist(self.app.encrypt_wifi)

        logging.info('步骤2.可用WLAN列表中点击wifi进行连接')
        target_wifi = device.get_element_by_text(self.app.encrypt_wifi)
        device.click_element(target_wifi)
        device.refresh_layout()
        pwd_area = device.get_element_by_type('TextInput')
        eye_icon = device.get_element_by_type('SymbolGlyph')
        device.click_element(eye_icon)
        px, py = device.center_of_element(pwd_area)
        device.input_text(px, py, self.app.wifi_pwd)
        device.close_soft_keyboard()
        device.refresh_layout()
        device.click_element(device.get_element_by_text('连接'))
        logging.info('预期结果2.输入正确密码，wifi连接成功，最上层有显示wifi图标')
        device.wait(20)
        device.refresh_layout()
        if device.get_element_by_text('密码错误，连接失败。') or device.get_element_by_text('知道了'):
            device.click_element(device.get_element_by_text('知道了'))
            device.click_element(device.get_element_by_text('连接'))
            device.wait(5)
            device.refresh_layout()
        device.assert_text_exist('已连接')
        device.hdc_shell('hidumper -s WifiDevice')
        status_bar0 = device.save_snapshot_to_local('statusbar0.jpeg')
        Images.crop_picture(status_bar0, *SystemUI.statusbar_area)
        assert Images.is_image_in_image(self.system_ui.wlan_icon, status_bar0), '状态栏没有wifi图标'

        logging.info('步骤3.1 点击已连接的wifi')
        device.click_element(device.get_element_by_text('已连接'))
        logging.info('预期结果3.1 点击连接的wifi有弹窗提示')
        device.refresh_layout()
        # 弹窗
        device.assert_type_exist('Dialog')
        # 二维码
        device.assert_type_exist('QRCode')
        device.assert_text_exist('扫一扫可连接此WLAN, 请注意隐私安全')
        device.assert_text_exist('状态信息')
        device.assert_text_exist('已连接')
        device.assert_text_exist('信号强度')
        device.assert_text_exist('频率')
        device.assert_text_exist('加密类型')
        device.assert_text_exist('取消')
        device.assert_text_exist('删除')

        logging.info('步骤3.2 点击已点击删除按钮')
        device.click_element(device.get_element_by_text('删除'))
        logging.info('步骤3.2 点击删除后则断开连接，最上层图标也会消失')
        device.refresh_layout()
        device.hdc_shell('hidumper -s WifiDevice')
        device.assert_text_exist('已连接', expect=False)
        status_bar1 = device.save_snapshot_to_local('statusbar1.jpeg')
        Images.crop_picture(status_bar1, *SystemUI.statusbar_area)
        is_in = Images.is_image_in_image(self.system_ui.wlan_icon, status_bar1)
        assert not is_in, '状态栏wifi图标未消失'
